package net.zjitc.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @Author admin
 * @Date 2019/4/24 9:32
 */
public class Test3 {
    public static void main(String[] args) {
        /*1、注册驱动
         * 2、获得数据库连接
         * 3、获得能操作sql语句的对象
         * 4、执行sql语句
         * 5、循环操作，逐条取出结果集对象中的数据
         * 6、关闭*/
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/lgy";
            String user="root";
            String password="123456";
            connection= DriverManager.getConnection(url,user,password);
            statement=connection.createStatement();
            String sql="SELECT * FROM USER";
            resultSet=statement.executeQuery(sql);
            System.out.println("id\tname\tage");
            while (resultSet.next()){
                int id=resultSet.getInt("id");
                String name=resultSet.getString("name");
                String age=resultSet.getString("age");
                System.out.println(id+"\t"+name+"\t"+age);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
